#!/bin/sh

## Script is designed for using with deadbeef-infobar -like plugins, still can be used in command line, see arguments
## below this commentary.
##
## Requires:
### - curl (may be replaced with other downloaders like wget)
### - xmllint (included in libxml2)
### - xmlstarlet (required for removing video links, may be omitted from the formatting pipe)
### - w3m (for human-friendly output formatting)

ARTIST=$1
TITLE=$2
# ALBUM=$3

ARTIST=`echo $ARTIST | tr '[:blank:]' '_'`
TITLE=`echo $TITLE | tr '[:blank:]' '_'`

XML_XPATH="//div[@class=\"lyrics-body\"]"
XML_XPATH_EXC="//div[@id=\"video-musictory\"]"

URL_TEMP="https://www.lyricsmania.com/${TITLE}_lyrics_${ARTIST}.html"
URL_TEMP=`echo ${URL_TEMP} | tr '[:upper:]' '[:lower:]'`

curl -s "${URL_TEMP}" | xmllint --html -xpath "${XML_XPATH}" - 2>/dev/null | xmlstarlet edit -d "$XML_XPATH_EXC" | w3m -dump -T text/html

exit 0;
